import re
import datetime

pattern = '''(?P<remote>[\d.]{7,}) - - \[(?P<datetime>[\w/: +]+)\] "(?P<method>\w+) (?P<url>\S+) (?P<protocol>[\w\d/.]+)\
" (?P<status>\d+) (?P<length>\d+) ".+" "(?P<useragent>.+)" ".+"'''

regex = re.compile(pattern)

conversion = {
    'datetime': lambda timestr: datetime.datetime.strptime(timestr, '%d/%b/%Y:%H:%M:%S %z'),
    'status': int,
    'length': int
}


def extract(logline: str):
    m = regex.match(logline)
    if m:
        return {k: conversion.get(k, lambda x: x)(v) for k, v in m.groupdict().items()}


def load_file(filename: str, encoding='utf-8'):
    """装载日志文件"""
    with open(filename, encoding=encoding) as f:
        for line in f:
            fields = extract(line)
            if fields:
                yield fields
            else:
                continue


src = load_file('access_2021_02_22.log')


def ip_url_handle(src):
    ips = {}
    urls = {}
    count = 0
    for data in src:
        ip = data['remote']
        ips[ip] = ips.get(ip, 0) + 1
        count += 1
        url = data['url']
        urls[url] = urls.get(url, 0) + 1
    print(ips)
    print(urls)


ip_url_handle(src)

"""
可能是把http_code及请求返回码理解为请求的ip了，不过稍微改下就行了，完成的不错
"""
